package org.dromara.system.mq.config;

import org.dromara.system.config.GlobalConfig;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class EmailQueueConfig {

    // 定义延时队列的交换机名称
    public static final String EMAIL_DELAYED_EXCHANGE_NAME = GlobalConfig.env + "email.delayed.exchange";
    // 定义延时队列的队列名称
    public static final String EMAIL_DELAYED_QUEUE = GlobalConfig.env + "email.delayed.queue";
    // 定义延时队列的路由键
    public static final String EMAIL_DELAYED_KEY = GlobalConfig.env + "email.delayed.key";

    // 创建延时队列的交换机
    @Bean
    public CustomExchange EmailDelayedExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(EMAIL_DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, arguments);
    }

    // 创建延时队列的队列
    @Bean
    public Queue emailDelayedQueue() {
        return QueueBuilder.durable(EMAIL_DELAYED_QUEUE).build();
    }

    // 将延时队列的队列绑定到交换机上
    @Bean
    public Binding emailDelayedBinding() {
        return BindingBuilder.bind(emailDelayedQueue()).to(EmailDelayedExchange()).with(EMAIL_DELAYED_KEY).noargs();
    }
}
